# Contributor: Joe Holden <jwh@zorins.us>
# Contributor: Michał Polański <michal@polanski.me>
# Maintainer: Michał Polański <michal@polanski.me>
pkgname=traefik
pkgver=2.2.8
pkgrel=0
pkgdesc="The Cloud Native Edge Router"
url="https://traefik.io"
arch="all !x86 !armhf !armv7 !mips !mips64" # tests fail on x86, armhf, and armv7; limited by go on mips
license="MIT"
makedepends="go go-bindata libcap"
install="$pkgname.pre-install"
pkgusers="$pkgname"
pkggroups="$pkgname"
subpackages="$pkgname-openrc"
options="chmod-clean"
source="$pkgname-$pkgver.tar.gz::https://github.com/containous/traefik/releases/download/v$pkgver/traefik-v$pkgver.src.tar.gz
	traefik.initd
	traefik.confd
	traefik.toml
	rate-limiter-test.patch"
builddir="$srcdir"

_codename=chevrotin
_build_timestamp=$(date -u "+%Y-%m-%dT%TZ" ${SOURCE_DATE_EPOCH:+-d @$SOURCE_DATE_EPOCH})

build() {
	go generate
	go build \
		-ldflags "
		-X github.com/containous/traefik/v2/pkg/version.Version=$pkgver
		-X github.com/containous/traefik/v2/pkg/version.Codename=$_codename
		-X github.com/containous/traefik/v2/pkg/version.BuildDate=$_build_timestamp
		" \
		-v -o bin/$pkgname \
		./cmd/$pkgname
}

check() {
	# unit tests
	go test ./...

	# integration tests
	cd integration
	go test -integration ./...

	# ensure correct version codename
	local codename_from_ci=$(grep "CODENAME" "$builddir"/.travis.yml | cut -d= -f2)
	[ "$_codename" = "$codename_from_ci" ] || die "outdated codename"
}

package() {
	install -Dm755 bin/$pkgname "$pkgdir"/usr/sbin/$pkgname
	setcap cap_net_bind_service=+ep "$pkgdir"/usr/sbin/$pkgname

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname

	install -dm750 -o $pkgname -g $pkgname "$pkgdir"/etc/$pkgname
	install -m644 -o $pkgname -g $pkgname "$srcdir"/traefik.toml \
		"$pkgdir"/etc/$pkgname/$pkgname.toml
}

cleanup_srcdir() {
	go clean -modcache
	default_cleanup_srcdir
}

sha512sums="b379a0408d98193ba6d77b5249f8a5ea8d0949e6ec7e817d1b114c4f3a00513a753b1d9c37019850da8af5f7352b7e717094d262b827cc618173403637324a30  traefik-2.2.8.tar.gz
563175a94d31fc6b64729781b5749eb1fd76dd0203a877e605fb2135e4095c29e81dc1f0a708d38d21f9200fa20fd957dd858501a4473d9471f4056a60a9bd7c  traefik.initd
872c671a67ef050cd5e798a1190653fede4e499dd29123a073e49ae551b308ac43b11973f472eef1a36775bdfbcbcd3926956b68d8362f8c5674d117b2f36dd4  traefik.confd
7dff62db55362433fe33a69bfb556e6f285a033aaaab46ea970ae4ee1b19a4b0d6b25bf5523d4dc6b40d26922945fd1263ae8e53d295d6bf7f18ea1477a9e4d5  traefik.toml
6b6f29fccef961647747b6a77c5b4f201a209e4dca9b21cc346236617076454f89f435bcb7a9f8cac04e3e8dca3b0e717ac5536ea96a283766d1b745eafb68b6  rate-limiter-test.patch"
